include ../config.mk

JOBS=$(shell grep -c ^processor /proc/cpuinfo)
ifeq ($(JOBS),)
JOBS = 1
endif

default: QM102V_CUSTOM

help:
	@echo "usage:"
	@echo "    make QM102V"
	@echo "    make QM102V_FAST"
	@echo "    make QM102V_AOV"
	@echo "    make QM102V_CUSTOM"
	@echo "    make menuconfig"

QM102V:
	if [ ! -d linux-4.9 ]; then tar xf linux-4.9.tar.gz; fi;\
	cd linux-4.9; export PATH=$$PATH:$(BSP_PATH)/tools;\
	export ARCH=arm; export CROSS_COMPILE=$(CROSS_COMPILE);\
	if [ ! -e .config ]; then make qm102v_defconfig; fi;\
	cp arch/arm/mach-fh/qm102v/board_config.h arch/arm/mach-fh/include/mach/board_config.h;\
	make uImage -j$(JOBS); \
	if [ -e arch/arm/boot/uImage_qm102v ]; then\
	    cp arch/arm/boot/uImage_qm102v arch/arm/boot/uImage;\
	fi;

QM102V_FAST:
	if [ ! -d linux-4.9 ]; then tar xf linux-4.9.tar.gz; fi;\
	cd linux-4.9; export PATH=$$PATH:$(BSP_PATH)/tools;\
	export ARCH=arm; export CROSS_COMPILE=$(CROSS_COMPILE);\
	if [ ! -e .config ]; then make qm102v_fast_defconfig; fi;\
	cp arch/arm/mach-fh/qm102v/board_config.h arch/arm/mach-fh/include/mach/board_config.h;\
	make uImage -j$(JOBS); \
	if [ -e arch/arm/boot/uImage_qm102v_fast ]; then\
	    cp arch/arm/boot/uImage_qm102v_fast arch/arm/boot/uImage;\
	fi;

QM102V_AOV:
	if [ ! -d linux-4.9 ]; then tar xf linux-4.9.tar.gz; fi;\
	cd linux-4.9; export PATH=$$PATH:$(BSP_PATH)/tools;\
	export ARCH=arm; export CROSS_COMPILE=$(CROSS_COMPILE);\
	if [ ! -e .config ]; then make qm102v_aov_defconfig; fi;\
	cp arch/arm/mach-fh/qm102v/board_config.h arch/arm/mach-fh/include/mach/board_config.h;\
	make uImage -j$(JOBS); \
	if [ -e arch/arm/boot/uImage_qm102v_aov ]; then\
	    cp arch/arm/boot/uImage_qm102v_aov arch/arm/boot/uImage;\
	fi;

QM102V_CUSTOM:
	if [ ! -d linux-4.9 ]; then tar xf linux-4.9.tar.gz; fi;\
	cd linux-4.9; export PATH=$$PATH:$(BSP_PATH)/tools;\
	export ARCH=arm; export CROSS_COMPILE=$(CROSS_COMPILE);\
	echo "CONFIG_KERNEL_DEFCONFIG: $(CONFIG_KERNEL_DEFCONFIG)"; \
	if [ ! -e .config ]; then make $(CONFIG_KERNEL_DEFCONFIG); fi;\
	cp arch/arm/mach-fh/qm102v/$(CONFIG_KERNEL_BOARD_NAME)/board_config.h arch/arm/mach-fh/include/mach/board_config.h || exit $?; \
	make uImage -j$(JOBS); \
	if [ -e arch/arm/boot/uImage_qm102v ]; then\
	    cp arch/arm/boot/uImage_qm102v arch/arm/boot/uImage;\
	fi;


menuconfig:
	@if [ ! -d linux-4.9 ]; then tar xf linux-4.9.tar.gz; fi
	@cd linux-4.9; if [ ! -e .config ]; then echo "run [cd linux-4.9; make ARCH=arm xxx_defconfig] first !"; exit 1; fi; \
    make ARCH=arm menuconfig

clean:
	cd linux-4.9 && make clean

distclean:
	cd linux-4.9 && make distclean